const app = new getApp();
var wxParse = require('../../../wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    default_img: {},
    topic_list: [],
    promotion_id: '',
    //轮播图属性
    adv_img: [],
    autoplay: true,
    interval: 3000,
    duration: 1000,
    circular: true,
    indicator_color: '#AAA',
    indicator_active_color: '#FFF',
    swiper_height: 150,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    // 加载广告位
    app.sendRequest({
      url: 'System.Shop.advDetail',
      data: {
        ap_keyword: 'APPLET_TOPIC_SWIPER',
        export_type: 'data'
      },
      success: function (res) {
        if (res.code == 0) {
          let data = res.data;
          data.advs = app.checkEmpty(data.advs, []);
          for (let index in data.advs) {
            data.advs[index].adv_image = app.IMG(data.advs[index].adv_image);
            if (index == 0) {
              app.getWindowSize(that, true, data.advs[index].adv_image);
            }
          }

          that.setData({
            adv_img: data.advs,
          })
        }
      }
    });
    app.defaultImg(that, true);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  loadInfo: function () {
    let that = this;
    that.loadTopic();
  },

  loadTopic: function () {
    let that = this;

    // 加载专题
    app.sendRequest({
      url: 'System.Goods.promotionTopic',
      data: {},
      success: function (res) {
        let code = res.code;
        let data = res.data;
        console.log(res);
        if (code == 0) {
          let topic_list = data.data;
          let topic_content = [];
          if (app.checkArray(topic_list)) {
            for (let index in topic_list) {
              //专题图片处理
              topic_list[index].picture_img = app.IMG(topic_list[index].picture_img);
              //富文本格式转化
              topic_content[index] = topic_list[index].introduce;
              wxParse.wxParse('topic_content[' + index + ']', 'html', topic_content[index], that, 5);
            }
          }

          that.setData({
            topic_list: topic_list,
            is_load: 1
          })
        }
      }
    });
  },

  /**
   * 图片加载获取高度
   */
  imgLoad: function(e) {
    let res = wx.getSystemInfoSync();
    let height = e.detail.height;
    let width = e.detail.width;
    let rate = width / height;
    let swiper_height = res.windowWidth / rate;

    this.setData({
      swiperHeight: swiper_height
    })
  },

  /**
   * 链接跳转
   */
  linkJump: function (e) {
    let url = e.currentTarget.dataset.url;
    app.linkJumpDetection(url);
  }
})